<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="450px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
        <el-col :span="24">
          <el-form-item label="生产日期">
            <el-date-picker
              style="width: 250px"
              v-model="daterangeMoeScrq"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
          <el-col :span="24">
            <el-form-item label="制造单号" prop="moeZzdh">
              <el-input v-model="queryParams.moeZzdh" placeholder="请输入制造单号" style="width: 250px" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="物料代码" prop="moeWldm">
              <el-input v-model="queryParams.moeWldm" placeholder="请输入物料代码" style="width: 250px" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="机台编码" prop="moeJtbh">
              <el-input v-model="queryParams.moeJtbh" placeholder="请输入机台编码" style="width: 250px" clearable />
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" @click="handleSearch" size="small">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['production:productMaterialAnalyse:export']"
          @click="handleExport"
          size="small">
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['production:productMaterialAnalyse:export']"
          @click="handleExportChild('#ProductMaterialTotal', '工单用料统计分析-统计信息')"
          size="small">
          统计信息
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 9px; top: 20px">
        <Page
          v-if="activeTab === 'first'"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          id="ProductMaterialAnalyseDetail"
          :height="tableH"
          :data="moeDetList"
          v-loading="loading"
          :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="生产日期" align="center" width="80" prop="moeScrq" />
          <el-table-column label="机台" align="center" width="87" prop="moeJtbh" />
          <el-table-column label="制造单号" align="center" width="130" prop="moeZzdh" />
          <el-table-column label="物料代码" align="center" prop="moeWldm" width="130" />
          <el-table-column label="品名规格" align="center" :show-overflow-tooltip="true" prop="moePmgg" />
          <el-table-column label="计划数量" align="center" prop="moeScsl" width="90" />
          <el-table-column label="良品数量" align="center" prop="moeLpsl" width="90" />
          <el-table-column label="不良数量" align="center" prop="moeBlsl" width="90" />
          <el-table-column label="标准毛重" align="center" prop="moeBzjz" width="90" />
          <el-table-column label="实际毛重" align="center" prop="moeBzjz" width="90" />
          <el-table-column label="需求领料" align="center" prop="moeXqzl" width="90" />
          <el-table-column label="已耗用料" align="center" prop="moeYhzl" width="90" />
          <el-table-column label="剩余需求" align="center" prop="moeSyxq" width="90" />
          <el-table-column label="超重损耗" align="center" prop="moeCzsh" width="90" />
          <el-table-column label="正数补料" align="center" prop="moeZsbl" width="90" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="统计信息" name="second">
        <el-table border stripe id="ProductMaterialTotal" :height="tableH" :data="moeDetTotal" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="生产日期" align="center" width="90" prop="moeScrq" />
          <el-table-column label="计划数量" align="center" prop="moeScsl" width="90" />
          <el-table-column label="良品数量" align="center" prop="moeLpsl" width="90" />
          <el-table-column label="不良数量" align="center" prop="moeLpsl" width="90" />
          <el-table-column label="需求领料" align="center" prop="moeXqzl" width="90" />
          <el-table-column label="已耗用料" align="center" prop="moeYhzl" width="90" />
          <el-table-column label="剩余需求" align="center" prop="moeSyxq" width="90" />
          <el-table-column label="超重补耗" align="center" prop="moeCzsh" width="90" />
          <el-table-column label="正数补料" align="center" prop="moeZsbl" width="90" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { exportTable } from '@/utils/ruoyi'
import { exportProductMaterialAnalyse, listProductMaterialAnalyse } from '@/api/production/productMaterialAnalyse'

export default {
  name: 'ProductMaterialAnalyse',
  data() {
    return {
      // 遮罩层
      loading: true,
      exportLoading: false,
      tableH: document.body.clientHeight - 300,
      activeTab: 'first',
      moeDetList: [],
      moeDetTotal: [],
      // 显示搜索条件
      openSearch: false,
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层Rq
      open: false,
      // 建立日期时间范围
      daterangeMoeScrq: [this.$moment().startOf('month').toDate(), this.$moment().add(100, 'year').endOf('month').toDate()],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        moeKsscrq: null,
        moeJsscrq: null,
        moeZzdh: null,
        moeWldm: null,
        moeJtbh: null,
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true
      if (null != this.daterangeMoeScrq && '' != this.daterangeMoeScrq) {
        this.queryParams.moeKsscrq = this.moment(this.daterangeMoeScrq[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.moeJsscrq = this.moment(this.daterangeMoeScrq[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      listProductMaterialAnalyse(this.queryParams)
        .then((response) => {
          // 明细
          this.moeDetList = response.data.detail.rows
          this.total = response.data.detail.total
          // 统计
          this.moeDetTotal = response.data.total
          this.loading = false
        })
        .catch((error) => {
          console.log(error)
          this.loading = false
        })
    },
    handleSearch() {
      this.openSearch = true
      this.title = '查询'
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    // 表单重置
    reset() {
      this.form = {
        moeZzdh: null,
        moeWldm: null,
        moeJtbh: null,
      }
      this.resetForm('form')
    },
    pageChange(page) {
      this.queryParams.pageNum = page
      this.getList()
    },
    /** 导出上表 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否导出工单用料统计分析明细数据？', '确认导出', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportProductMaterialAnalyse(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
    /** 导出下表 */
    handleExportChild(id, title) {
      this.$confirm('是否导出' + title + '?', '确认导出', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning',
      })
        .then(function () {
          return exportTable(id, title)
        })
        .catch(() => {})
    },
  },
}
</script>

<style scoped></style>
